package in.muton.edushare.grading.panels.studentGrades;

import in.muton.edushare.academics.alerts.ComponentAlert;
import in.muton.edushare.academics.alerts.FacultyGradeMail;
import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.grading.panels.studentGrades.GradingUtil.ItemGradeHelper;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.edushare.wicket.models.SimpleList;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.vo.GradingItem;
import com.edushare.common.vo.Users;

public class StudentItemEdit extends EdusharePanel {
	private static final long serialVersionUID = 2092150832411962429L;
	private GradingUtil gUtil;
	private StudentMainGradePanel mainPanel = null;
	private boolean initialized = false;
	private List<ItemGradeHelper> ighList = new ArrayList<ItemGradeHelper>();

	public StudentItemEdit(String id, EduShareBasePage page,
			final StudentMainGradePanel mainPanel) {
		super(id, page);
		this.mainPanel = mainPanel;
		gUtil = new GradingUtil(mainPanel.getcInstModel());

		add(new Label("itemName", new Model<String>() {
			private static final long serialVersionUID = 8850928605405817935L;

			@Override
			public String getObject() {
				if (mainPanel.getActiveMode() == StudentMainGradePanel.EDIT_ITEM_GRADES) {
					return mainPanel.getiGradeModel().getObject()
							.getGradingItemName();
				}
				return "";
			}
		}));

		Form<Void> f = new Form<Void>("itemForm") {
			private static final long serialVersionUID = -3974793423678458531L;

			@Override
			public void onSubmit() {
				List<ItemGradeHelper> ighL = getGrades();
				boolean error = false;
				FacultyGradeMail fm = new FacultyGradeMail(mainPanel.getcInstModel(), new CayenneModel<Users>(getLoggedInUser()));
				for (ItemGradeHelper igh : ighL) {
					if (!gUtil.isValidMarks(igh.marks, igh.gradingItem
							.getObject())) {
						setError("The marks you entered are greater than maximum for student "
								+ igh.user.getObject().getCompleteUserName());
						error = true;
					} else {
						double orig_marks = gUtil.getGradesByItemUser(igh.user
								.getObject(), igh.gradingItem.getObject()).marks;
						gUtil.saveMarks(igh);
						
						if(orig_marks != igh.marks){
							new ComponentAlert(igh).sendAlerts();
							fm.addAlert(igh);
						}
					}
				}
				if (!error) {
					initialized = false;
					mainPanel
							.setActiveMode(StudentMainGradePanel.VIEW_ITEM_GRADES);
				}
				fm.sendAlerts();
			}
		};
		add(f);
		f.add(new Button("gcCancel") {
			private static final long serialVersionUID = 6781782741606748851L;

			@Override
			public void onSubmit() {
				mainPanel.setActiveMode(StudentMainGradePanel.VIEW_ITEM_GRADES);
			}
		}.setDefaultFormProcessing(false));

		f.add(new ListView<ItemGradeHelper>("itemGradeLV",
				new SimpleList<ItemGradeHelper>() {
					private static final long serialVersionUID = -1221539779466090807L;

					@Override
					public List<ItemGradeHelper> getObject() {
						return getGrades();
					}
				}) {
			private static final long serialVersionUID = -960622261185663414L;

			@Override
			protected void populateItem(final ListItem<ItemGradeHelper> arg0) {
				boolean isVisible = StudentItemView.isStudentInSearch(arg0
						.getModelObject().user.getObject(), mainPanel
						.getStudentName());
				arg0.setVisible(isVisible);
				arg0.add(new EduShareURLPanel("studentName", arg0
						.getModelObject().user));
				arg0.add(new TextField<Double>("grade",
						new PropertyModel<Double>(arg0.getModelObject(),
								"marks"), Double.class));
				arg0.add(new Label("max", getMaxMarksString()));
			}
		});
	}

	public List<ItemGradeHelper> getGrades() {
		if (!initialized) {
			IModel<GradingItem> item = mainPanel.getiGradeModel();
			if (item != null) {
				ighList = gUtil.getMarkList(item.getObject());
				initialized = true;
			}
		}
		return ighList;
	}

	@Override
	public boolean isVisible() {
		CourseRoleCheck crc = new CourseRoleCheck(mainPanel.getcInstModel()
				.getObject(), getLoggedInUser());
		return mainPanel.getActiveMode() == StudentMainGradePanel.EDIT_ITEM_GRADES
				&& (crc.isAdmin() || crc.isTeacher());
	}

	public boolean isInitialized() {
		return initialized;
	}

	public void setInitialized(boolean initialized) {
		this.initialized = initialized;
	}

	public String getMaxMarksString() {
		IModel<GradingItem> item = mainPanel.getiGradeModel();
		double maxMarks = 0.0;
		if (item != null && item.getObject().getMaxMarks() != null) {
			maxMarks = item.getObject().getMaxMarks();
			return " of " + maxMarks;
		} else {
			return "";
		}
	}
}
